package com.neu.af.dataFilter.service.impl;

import com.neu.af.dataFilter.entity.*;
import com.neu.af.dataFilter.mapper.*;

import com.neu.af.dataFilter.service.AbDataService;
import com.neu.af.dataFilter.service.piservice.PiRead;
import com.neu.af.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("abDataService")
public class AbDataServiceImpl implements AbDataService {
    @Autowired
    AbDataMapper abDataMapper;
    @Autowired
    DataMapper dataMapper;
    @Autowired
    TagRuleAlarmMapper tagRuleAlarmMapper;
    @Autowired
    MonitorStatusMapper monitorStatusMapper;
    @Autowired
    TagMapper tagMapper;
    @Autowired
    PiRead piRead;
    @Autowired
    JizuMapper jizuMapper;
    @Autowired
    KoujingMapper koujingMapper;
    @Autowired
    DataStatsMapper dataStatsMapper;
    @Autowired
    DataStatsDayMapper dataStatsDayMapper;
//    @Autowired
//    AsyncTaskService asyncTaskService;

    @Override
    public int insert(AbData abData) {
        return abDataMapper.insert(abData);
    }

    @Override
    public int update(AbData abData) {
        return abDataMapper.update(abData);
    }

    @Override
    public int delete(String id) {
        return abDataMapper.delete(id);
    }

    @Override
    public AbData getById(String data_id) {
        return abDataMapper.getById(data_id);
    }

    @Override
    public List<AbData> getAll() {
        return abDataMapper.getAll();
    }

    @Override
    public List<AbData> getAllByFilter(Map<String, Object> map) {
        return abDataMapper.getAllByFilter(map);
    }

    @Override
    public String[] initsList(int p) {
        String[] sList = new String[p];
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i<p; i ++) {
            map.put("start", i);
            map.put("end", i+1);
            List<String> strings = tagMapper.getAllName(map);
            String s = "";
            for (String ss: strings) {
                s = s+"tag='"+ss+"' or ";
            }
            s =s.substring(0,s.length()-4);
            sList[i] = s;
        }
        return sList;
    }

    @Override
    public List<AbData> saveAbnormalDataOneTurn(List<Data> data, TagRuleAlarm tagRuleAlarm) {
        String ruleValue = tagRuleAlarm.getRule_value();
        if(ruleValue==null) {
            return null;
        }
        String contains = tagRuleAlarm.getContains();
        // 对data进行规则过滤
        ArrayList<AbData> abData = new ArrayList<>();
        if (contains.equals(">") || contains.equals("大于")) {
            float floatValue = Float.parseFloat(ruleValue);

            for(Data d: data) {
                AbData a = new AbData();
                if(abDataMapper.selectByNameAndTime(d).size()==0) continue;
                if (d.getData().equals("System.__ComObject")) a.setAlarm_class(3);
                else if( Float.parseFloat(d.getData()) > floatValue)
                    a.setAlarm_class(tagRuleAlarm.getAlarm_class());
                else continue;
                a.setType(2);
                a.setThreshold(ruleValue);
                a.setTag_name(tagRuleAlarm.getTag_name());
                a.setDescriptor(tagRuleAlarm.getDescriptor());
                a.setData(d.getData());
                a.setCreate_time(d.getCreate_time());
                a.setDescriptor(tagRuleAlarm.getDescriptor());
                abDataMapper.insert(a);
//                a.setId(abDataMapper.getMaxId());
                if(a.getAlarm_class() != 3) abData.add(a);
            }
        }
        else if(contains.equals("<") || contains.equals("小于")) {
            float floatValue = Float.parseFloat(ruleValue);

            for(Data d: data) {
                AbData a = new AbData();
                if(abDataMapper.selectByNameAndTime(d).size()==0) continue;
                if (d.getData().equals("System.__ComObject")) a.setAlarm_class(3);
                else if ( Float.parseFloat(d.getData()) < floatValue)
                    a.setAlarm_class(tagRuleAlarm.getAlarm_class());
                else continue;
                a.setType(2);
                a.setThreshold(ruleValue);
                a.setTag_name(tagRuleAlarm.getTag_name());
                a.setDescriptor(tagRuleAlarm.getDescriptor());
                a.setData(d.getData());
                a.setCreate_time(d.getCreate_time());
                a.setDescriptor(tagRuleAlarm.getDescriptor());
                abDataMapper.insert(a);
//                a.setId(abDataMapper.getMaxId());
                if(a.getAlarm_class() != 3) abData.add(a);
            }
        }
        else {
            for (Data d: data) {
                AbData a = new AbData();
                if(abDataMapper.selectByNameAndTime(d).size()==1) continue;
                if(d.getData().equals("System.__ComObject")) a.setAlarm_class(3);
                else if( d.getData().equals("-1"))
                    a.setAlarm_class(tagRuleAlarm.getAlarm_class());
                else continue;
                a.setType(1);
                a.setTag_name(tagRuleAlarm.getTag_name());
                a.setDescriptor(tagRuleAlarm.getDescriptor());
                a.setData(d.getData());
                a.setCreate_time(d.getCreate_time());
                a.setDescriptor(tagRuleAlarm.getDescriptor());
                abDataMapper.insert(a);
//                a.setId(abDataMapper.getMaxId());
                if(a.getAlarm_class() != 3) abData.add(a);
            }
        }
        return abData;
    }

    @Override
    public int saveAbnormalDataOneTag(Data data) {
        TagRuleAlarm tagRuleAlarm = tagRuleAlarmMapper.getByTagName(data.getTag_name());
        if(tagRuleAlarm == null) return 0;
        String ruleValue = tagRuleAlarm.getRule_value();
        if(ruleValue==null) {
            return 0;
        }
        String contains = tagRuleAlarm.getContains();

        long differerce;
        Timestamp timestamp = abDataMapper.getMaxCreatTimeByTagName(data.getTag_name());
        if(timestamp == null || timestamp.equals(0)) differerce = koujingMapper.getKoujing();
        else differerce = ((data.getCreate_time().getTime() - timestamp.getTime())/60000);

//        System.out.println(timestamp.getTime());
//        System.out.println(differerce);
        // 对data进行规则过滤
        AbData a = new AbData();
        if (contains.equals(">") || contains.equals("大于")) {
            float floatValue = Float.parseFloat(ruleValue);
            if(abDataMapper.selectByNameAndTime(data).size()!=0) return 0;
            if (data.getData().equals("System.__ComObject")) return 0;
            else if( Float.parseFloat(data.getData()) > floatValue)
                a.setAlarm_class(tagRuleAlarm.getAlarm_class());
            else return 0;
            if (differerce<koujingMapper.getKoujing())return 0;
            a.setType(2);
            a.setThreshold(ruleValue);
            a.setTag_name(tagRuleAlarm.getTag_name());
            a.setDescriptor(tagRuleAlarm.getDescriptor());
            a.setData(data.getData());
            a.setCreate_time(data.getCreate_time());
            a.setDescriptor(tagRuleAlarm.getDescriptor());
            abDataMapper.insert(a);
            countStats(a);
            monitorStatusMapper.updateOneAbnormalTag(data.getTag_name());
        }
        else if (contains.equals("<") || contains.equals("小于")) {
            float floatValue = Float.parseFloat(ruleValue);
            if(abDataMapper.selectByNameAndTime(data).size()!=0) return 0;
            if (data.getData().equals("System.__ComObject")) return 0;
            else if( Float.parseFloat(data.getData()) < floatValue)
                a.setAlarm_class(tagRuleAlarm.getAlarm_class());
            else return 0;
            if (differerce<koujingMapper.getKoujing())return 0;
            a.setType(2);
            a.setThreshold(ruleValue);
            a.setTag_name(tagRuleAlarm.getTag_name());
            a.setDescriptor(tagRuleAlarm.getDescriptor());
            a.setData(data.getData());
            a.setCreate_time(data.getCreate_time());
            a.setDescriptor(tagRuleAlarm.getDescriptor());
            abDataMapper.insert(a);
            countStats(a);
            monitorStatusMapper.updateOneAbnormalTag(data.getTag_name());
        }

        else {
            if(abDataMapper.selectByNameAndTime(data).size()!=0) return 0;
            if(data.getData().equals("System.__ComObject")) return 0;
            else if( data.getData().equals("-1"))
                a.setAlarm_class(tagRuleAlarm.getAlarm_class());
            else return 0;
            if (differerce<koujingMapper.getKoujing())return 0;
            a.setType(1);
            a.setTag_name(tagRuleAlarm.getTag_name());
            a.setDescriptor(tagRuleAlarm.getDescriptor());
            a.setData(data.getData());
            a.setCreate_time(data.getCreate_time());
            a.setDescriptor(tagRuleAlarm.getDescriptor());
            abDataMapper.insert(a);
            countStats(a);
            monitorStatusMapper.updateOneAbnormalTag(data.getTag_name());
//                a.setId(abDataMapper.getMaxId());
        }
        return 1;
    }


    // @PostConstruct
    public void run() {
        ExecutorService executorService= Executors.newFixedThreadPool(6);
        // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        int i = 0;

        for(int j = 0; j < 3; j++) {

            piRead.test();
        }

        String[] tagListForThread = initsList(6);

        while(true) {
            if (i == 360000) {
                i = 0;
                monitorStatusMapper.updateAllNormalStatus();
            }
            int finalI = i % 6;
            int finalI2 = i% 72;
            executorService.execute(()->{
                if (finalI2 == 5) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("start", 5);
                    map.put("end", 6);
                    List<String> strings = tagMapper.getAllName(map);
                    String s = "";
                    for (String ss : strings) {
                        s = s + "tag='" + ss + "' or ";
                    }
                    s = s.substring(0, s.length() - 4);
                    getAbnormalDataMultiThread2(s);
                }
                getAbnormalDataMultiThread2(tagListForThread[finalI]);
                System.out.println("done");
                System.out.println(new Date());
            });

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
        }
    }

//     @PostConstruct
//    public void run() {
//        piRead.test();
//    }


    @Override
    public int selectByNameAndTime(Data data) {
        List<AbData> abData = abDataMapper.selectByNameAndTime(data);
        if(abData==null || abData.size() == 0) return 0;
        return 1;
    }

    @Override
    public void getAbnormalData(int interval) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        int pp = 0;
        Map<String, Object> map11 = new HashMap<>();
//        map11.put("start", 0);
//        map11.put("end", 1);
        List<String> tagList = tagMapper.getAllName(map11);
        while(true) {
            if (pp == 180) {
                pp = 0;
                monitorStatusMapper.updateAllNormalStatus(); // 假设全为正常的
            }
            if(abDataMapper.getAll().size()>200000) {
                abDataMapper.delete3000();
            }

            map11.put("start", pp%6);
            map11.put("end",(pp+1)%6);
            tagList = tagMapper.getAllName(map11);

//            for(int p = 0; p < 7; p ++) {
            piRead.test();
            System.out.println(new Date());

//                Map<String, Object> map11 = new HashMap<>();
//                map11.put("start",p);
//                map11.put("end", p+1);

//                String[] tagList1 = new String[]{"BSTP:0110HAA20CL008L1"};
//                List<String> tagList = Arrays.asList(tagList1);

            for(String tag: tagList) {
                // System.out.println(tag.split(":")[1]);
                String lastTime = df.format(new Date().getTime()-120*1000);
                String res = piRead.GetData("tag='BSTP:"
                        +tag.split(":")[1]+"'",lastTime," ");
//                    String res = "";
//                    for(String s:stringDataOneTag) {
//                        res += s;
//                    }
                // System.out.println(res);
                if(res.equals("0")) continue;
                List<Data> allDataOneTag = DataUtil.piString2Data(res);
                List<Data> dataNullOneTag = DataUtil.filterDataNull(allDataOneTag); // 缺省数据
                // 数据全为空
                TagRuleAlarm tt = tagRuleAlarmMapper.getByTagName(tag);
                if (tt!=null && tt.getDescriptor()!= null && dataNullOneTag != null && dataNullOneTag.size() == allDataOneTag.size() && !res.equals("0") && !tt.getDescriptor().contains("停机")) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("tag_name",tag);
                    map.put("record_amount",allDataOneTag.size());
                    Timestamp minTime = DataUtil.getMinCreateTimeD(allDataOneTag);
                    map.put("first_create_time",minTime);
                    monitorStatusMapper.updateByTagNameWithDatasourceInterupt(map);
                }
//                List<Data> data = DataUtil.filterByTagName(allDataOneTag, tag); // 单标签所有数据
//                List<Data> dataNullTagName = DataUtil.filterDataNull(data); // 单标签缺省数据
                TagRuleAlarm tagRuleAlarm = tagRuleAlarmMapper.getByTagName(tag);
                // 无规则对应tag，说明不是异常监测点
                if (tagRuleAlarm == null) continue;

                List<AbData> abDataTagName = this.saveAbnormalDataOneTurn(allDataOneTag, tagRuleAlarm);
//
                // 如果当前标签有异常数据且不全为缺省，装入异常数据列表并更新检测点状态为异常
                if(abDataTagName != null && abDataTagName.size()!=0) {
                    // System.out.println(111);
                    int record_amount = abDataTagName.size();
                    monitorStatusMapper.initByTagName(tag);
                    Timestamp minTime = DataUtil.getMinCreateTime(abDataTagName);
                    MonitorStatus m = monitorStatusMapper.getByTagName(tag);
                    m.setFirst_create_time(minTime);
//                    System.out.println(tag+"   mintime");
//                    System.out.println(minTime);
                    monitorStatusMapper.updateFirstCreateTimeByTagName(m);
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("tag_name", tag);
                    map.put("record_amount", record_amount);

                    // map.put("first_create_time", abDataMapper.getMinCreateTimeByTagName(tagName));
                    ///map.put("first_create_time", DataUtil.getMinCreateTime(abDataTagName).toString());
                    monitorStatusMapper.updateByTagNameWithAbStatus(map);
                }

            }

            piRead.close();
            System.out.println(new Date());
            pp++;


            // System.out.println("ppppppppppppppppppppppppppppppppppppp    ");
            System.out.println(pp);

            try {
                Thread.sleep(interval*100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    @Async
    public void getAbnormalDataMultiThread(int interval, int tagIndex, SimpleDateFormat df) {
        System.out.println("线程" + Thread.currentThread().getName() + " 执行任务：" + tagIndex);
        System.out.println(new Date());
        Map<String, Object> map11 = new HashMap<>();
        map11.put("start", tagIndex);
        map11.put("end", tagIndex+1);
        List<String> tagList = tagMapper.getAllName(map11);

        for(String tag: tagList) {
            // System.out.println(tag.split(":")[1]);
            String lastTime = df.format(new Date().getTime()-45*1000);
            String res = piRead.GetData("tag='BSTP:"
                    +tag.split(":")[1]+"'",lastTime," ");
//                    String res = "";
//                    for(String s:stringDataOneTag) {
//                        res += s;
//                    }

            if(res.equals("0")) continue;
            // System.out.println(res);
            List<Data> allDataOneTag = DataUtil.piString2Data(res);
            List<Data> dataNullOneTag = DataUtil.filterDataNull(allDataOneTag); // 缺省数据
            // 数据全为空
            TagRuleAlarm tt = tagRuleAlarmMapper.getByTagName(tag);
            if (tt!=null && tt.getDescriptor()!= null && dataNullOneTag != null && dataNullOneTag.size() == allDataOneTag.size() && !res.equals("0") && !tt.getDescriptor().contains("停机")) {
                Map<String, Object> map = new HashMap<>();
                map.put("tag_name",tag);
                map.put("record_amount",allDataOneTag.size());
                Timestamp minTime = DataUtil.getMinCreateTimeD(allDataOneTag);
                map.put("first_create_time",minTime);
                monitorStatusMapper.updateByTagNameWithDatasourceInterupt(map);
            }
//                List<Data> data = DataUtil.filterByTagName(allDataOneTag, tag); // 单标签所有数据
//                List<Data> dataNullTagName = DataUtil.filterDataNull(data); // 单标签缺省数据
            TagRuleAlarm tagRuleAlarm = tagRuleAlarmMapper.getByTagName(tag);
            // 无规则对应tag，说明不是异常监测点
            if (tagRuleAlarm == null) continue;

            List<AbData> abDataTagName = this.saveAbnormalDataOneTurn(allDataOneTag, tagRuleAlarm);
//
            // 如果当前标签有异常数据且不全为缺省，装入异常数据列表并更新检测点状态为异常
            if(abDataTagName != null && abDataTagName.size()!=0) {
                // System.out.println(111);
                int record_amount = abDataTagName.size();
                monitorStatusMapper.initByTagName(tag);
                Timestamp minTime = DataUtil.getMinCreateTime(abDataTagName);
                MonitorStatus m = monitorStatusMapper.getByTagName(tag);
                m.setFirst_create_time(minTime);
//                    System.out.println(tag+"   mintime");
//                    System.out.println(minTime);
                monitorStatusMapper.updateFirstCreateTimeByTagName(m);
                HashMap<String, Object> map = new HashMap<>();
                map.put("tag_name", tag);
                map.put("record_amount", record_amount);

                // map.put("first_create_time", abDataMapper.getMinCreateTimeByTagName(tagName));
                ///map.put("first_create_time", DataUtil.getMinCreateTime(abDataTagName).toString());
                monitorStatusMapper.updateByTagNameWithAbStatus(map);
            }
        }
    }

    @Override
    @Async
    public void getAbnormalDataMultiThread2(String requstString) {
        System.out.println("线程" + Thread.currentThread().getName() + " 执行任务：" + requstString.length());
        System.out.println(abDataMapper.getAll().size());
        if(abDataMapper.getAll().size()>10000) {
            abDataMapper.delete3000();
            AbData a = new AbData();
            a.setDescriptor("2#炉热水出口集箱水温度二高高");
            abDataMapper.insert(a);
            a.setDescriptor("3#炉热水出口集箱水温度二高高");
            abDataMapper.insert(a);
            a.setDescriptor("1#炉热水出口集箱水温度二高高");
            abDataMapper.insert(a);
        }
        System.out.println(new Date());
        // 用pi读数据
        String dataString = piRead.getSnapShotDataFromPI(requstString, " ");
        List<Data> allData = DataUtil.piString2Data2(dataString);
        // 用txt读数据
        //String allDataString = DataUtil.readStringDataFromTxt("txt/test.txt").get(0);
        //System.out.println(allDataString);
        //List<Data> allData = DataUtil.piString2Data2(allDataString);

        if(allData.size()>1000) {
            List<Data> nullData = DataUtil.filterDataNull(allData);
            if(nullData.size()==allData.size()){
                monitorStatusMapper.updateSourceInterupt();
            }
        }

        for(Data d: allData) {
            saveAbnormalDataOneTag(d);
        }
    }

    /**
     *
     * @param serachMap: 包含搜索条件，上次搜索最大Id
     * @return
     * @author mfc
     * @date
     */
    @Override
    public List<AbData> getAlarmClassByType(Map<String, Object> serachMap) {

        int type = (int) serachMap.get("type");
        int alarmClass = (int) serachMap.get("alarmClass");
        int id = (int) serachMap.get("id");

        List<AbData> AbDataList = new ArrayList<AbData>();
        if(type==-2||alarmClass==-2){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.YEAR, -1);
            Date dd = c.getTime();
            String datetime = format.format(dd);
            AbDataList = abDataMapper.getAlarmClass(0,datetime);
        }else {
            if (id == -1) {

                AbDataList = abDataMapper.getAlarmClassByCreateTime(type, alarmClass);
            } else {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                c.add(Calendar.MINUTE, -1);
                Date dd = c.getTime();
                String datetime = format.format(dd);
                AbDataList = abDataMapper.getAlarmClass(id,datetime);
            }
        }
        return AbDataList;
    }

    @Override
    public Map<String,List<AbData>> getClassOneAlarm(Map<String, Object> serachMap) {

        int id = (int) serachMap.get("id");

        List<AbData> AbDataList = new ArrayList<AbData>();

        List<String> stringList = abDataMapper.getAlarmName();

        if (id == -1) {
            AbDataList = abDataMapper.getClassOneAlarmByCreateTime();
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MINUTE, -1);
            Date dd = c.getTime();
            String datetime = format.format(dd);
            AbDataList = abDataMapper.getClassOneAlarm(id,datetime);
        }
        String pattern = "\\d+(#炉)?(机组)?";

        // 创建 Pattern 对象String
        Pattern r = Pattern.compile(pattern);

        Map<String,List<AbData>> map = new HashMap<>();
        for(int i = 0;i<AbDataList.size();i++){
            AbData abData = AbDataList.get(i);
            // 现在创建 matcher 对象
            Matcher m = r.matcher(abData.getDescriptor());

            if(m.find()){
                if(map.containsKey(m.group(0))){
                    map.get(m.group(0)).add(abData);
                }else{
                    List<AbData> abDataList = new ArrayList<>();
                    abDataList.add(abData);
                    map.put(m.group(0),abDataList);
                }
            }

        }

        for(int i = 0;i<stringList.size();i++){
            String abData = stringList.get(i);
            // 现在创建 matcher 对象
            Matcher m = r.matcher(abData);

            if(m.find()){
                if(map.containsKey(m.group(0))){
                   continue;
                }else{
                    List<AbData> abDataList = new ArrayList<>();
                    map.put(m.group(0),abDataList);
                }
            }

        }

        return map;
    }

    @Override
    public List<AbData> getAlarmClassByGroup(Map<String, Object> serachMap) {
        String alarmGroup = (String) serachMap.get("alarmGroup");
        int id = (int) serachMap.get("id");
        List<AbData> AbDataList = new ArrayList<AbData>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MINUTE, 0-koujingMapper.getKoujing());
        Date dd = c.getTime();
        String datetime = format.format(dd);
        AbDataList = abDataMapper.getAlarmClassByGroup(alarmGroup,id,datetime);
        return AbDataList;
    }



    private void countStats(AbData a) {

        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM");
        String datetime = tempDate.format(new Date());
        SimpleDateFormat tempDateday = new SimpleDateFormat("yyyy-MM-dd");
        String datetimeday = tempDateday.format(new Date());
        insertDataStats(datetime,a,1);
        insertDataStats(datetimeday,a,2);
        // 按指定模式在字符串查找
        String pattern = "\\d+(#炉)?(机组)?";


        // 创建 Pattern 对象String
        Pattern r = Pattern.compile(pattern);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(a.getDescriptor());
        if (m.find( )) {

            if(!m.group(0).matches("[0-9]+")){
                insertJizuF(m,a,datetime);
            }
        }
    }

    private void insertDataStats(String datetime, AbData a, int i) {
        if(i==1) {
            DataStats dataStats = new DataStats();
            List<DataStats> dataStatsList = dataStatsMapper.searchBydatetime(datetime);
            if (a.getAlarm_class() == 1) {
                dataStats.setClassonenum(1);
                dataStats.setClasstwonum(0);
            } else {
                dataStats.setClassonenum(0);
                dataStats.setClasstwonum(1);
            }
            if (dataStatsList.size() == 0) {
                dataStats.setMonth(datetime);
                dataStats.setAllnum(1);
                dataStatsMapper.insertDataStats(dataStats);
            } else {
                dataStats.setId(dataStatsList.get(0).getId());
                dataStats.setAllnum(dataStatsList.get(0).getAllnum() + 1);
                dataStatsMapper.updateDataStats(dataStats);
            }
        }else{
            DataStatsDay dataStatsday = new DataStatsDay();
            List<DataStatsDay> dataStatsDayList = dataStatsDayMapper.searchDataStatsDayBydatetime(datetime);
            if (a.getAlarm_class() == 1) {
                dataStatsday.setClassonenum(1);
                dataStatsday.setClasstwonum(0);
            } else {
                dataStatsday.setClassonenum(0);
                dataStatsday.setClasstwonum(1);
            }
            if (dataStatsDayList.size() == 0) {
                dataStatsday.setDay(datetime);
                dataStatsday.setAllnum(1);
                dataStatsDayMapper.insertDataStatsDay(dataStatsday);
            } else {
                dataStatsday.setId(dataStatsDayList.get(0).getId());
                dataStatsday.setAllnum(dataStatsDayList.get(0).getAllnum() + 1);
                dataStatsDayMapper.updateDataStatsDay(dataStatsday);
            }
        }
    }

    private void insertJizuF(Matcher m, AbData a, String datetime) {
        Jizu jizu = new Jizu();
        String jizuName = m.group(0);
        if(a.getAlarm_class()==1){
            jizu.setClassonenum(1);
            jizu.setClasstwonum(0);
        }else{
            jizu.setClassonenum(0);
            jizu.setClasstwonum(1);
        }
        jizu.setMonth(datetime);
        jizu.setJizuname(jizuName);
        List<Jizu> jizuList = jizuMapper.searchByName(jizuName,datetime);
        if(jizuList.size()==0){
            jizu.setAlarmnum(1);
            jizuMapper.insertJizu(jizu);
        }else{
            jizu.setAlarmnum(jizuList.get(0).getAlarmnum()+1);
            jizu.setId(jizuList.get(0).getId());
            jizuMapper.updateJizu(jizu);
        }
    }

    @Override
    public void updateKoujing(Map<String, Object> serachMap) {
        int koujing = (int) serachMap.get("koujing");
        koujingMapper.updateKoujing(koujing);

    }

    @Override
    public int getKoujing(){
        return koujingMapper.getKoujing();
    }


}



